package database

import (
	"GOPAAS/configs/config"
	"fmt"
	"github.com/sirupsen/logrus"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

var DConfig = &gorm.Config{
	NamingStrategy: schema.NamingStrategy{
		SingularTable: true, // 使用单数表名，启用该选项后，`User` 表将是`user`
	},
	Logger: logger.Default.LogMode(logger.Info),
}

func connectPostGre(config *config.DatabaseConfig) *gorm.DB {
	dsn := "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai"
	dsn = fmt.Sprintf(dsn, config.Host, config.User, config.Password, config.DbName, config.Port)
	postGreDb, err := gorm.Open(postgres.Open(dsn), DConfig)
	if err != nil {
		logrus.Error(err)
		panic("failed to connect database")
	}
	return postGreDb
}
